<?php


include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/gonglue/inc.php");


class mod_cron_engine_main 
{
	//区别在于搜索结果页的分页方式不一样
	function get_test($value)
	{
		
				
				//抓第一条内容
				$detail = file_get_contents($value);
				if(!$detail)
				{
					continue;
				}
				
				preg_match_all("/\<div.*?=\"artArea\".*?\>(.*?)\<\/div\>[\s]+\<\/?div/is",$detail,$match1);
				
				
				echo "match:";
				
				
				//内容第一页内容
				$data_content = "";
				$length_get=0;
				foreach($match1[1] as $value_get)
				{
					
					$value_get = preg_replace("/\<a.*?\>.*?\<\/a\>/is","",$value_get);
					$value_get = preg_replace("/\<script.*?script\>/is","",$value_get);
					
					$word_get_length = strlen(preg_replace("/\<.*?\>/","",$value_get));
					
					if($word_get_length > $length_get)
					{
						$data_content = $value_get;
						$length_get = $word_get_length;
					}
				}
			
				//匹配分页
				$chck_url = preg_quote(substr($value,0,strlen($value)-5),"/");
				preg_match_all("/({$chck_url}_.*?html)/is",$detail,$match2);
				$get_all_page = array_unique($match2[1]);
				
				//遍历所有分页，取出内容
				foreach($get_all_page as $key1 => $value1)
				{
					$page_detail = file_get_contents($value1);
					preg_match_all("/\<div.*?=\"artArea\".*?\>(.*?)\<\/div\>[\s]+\<\/?div/is",$page_detail,$match1);
					$long_content ="";
					$length_get=0;
					foreach($match1[1] as $value3)
					{
						$value3 = preg_replace("/\<a.*?\>.*?\<\/a\>/is","",$value3);
						$value3 = preg_replace("/\<script.*?script\>/is","",$value3);
						
						$word_get_length = strlen(preg_replace("/\<.*?\>/","",$value3));
						if($word_get_length >$length_get)
						{
							$long_content = $value3;
							$length_get = $word_get_length;
						}
					}
					$data_content .= $long_content;
				}
				
				
				
				
				
		
	
	}
	
	//抓取太平洋游戏网 pcgames.com
	function get_taipingyang($list_url,$topic_id,$cron_get_time,$content_type,$p=1)
	{//http://wangyou.pcgames.com.cn/zhuanti/lol/gonglue/zsyanjiu/
		
		echo "get_page : {$list_url} <br>\n";
		//抓列表页第一页
		$content = file_get_contents($list_url);
		
		
		$db_base = new db_base();
		if($p == 1)
		{
			//处理列表页分页
			$list_quote_url = preg_quote($list_url,"/");
			preg_match_all("/href=\"{$list_quote_url}index_([0-9]+)\.html\"/is",$content,$match_pages);
			
			echo "time compare " . time() . " $cron_get_time <br>\n ";
			if(time() - $cron_get_time > 86400*30 ) //如果上次抓取在一星期之前，所有都抓一次
			{	
				if($match_pages[1])
				{
					$max_page = max($match_pages[1]);
					if($max_page > 100)$max_page=100;
					for($i=$max_page;$i>1;$i--)
					{
						$this->get_taipingyang("{$list_url}/index_{$i}.html",$topic_id,$cron_get_time,$content_type,$i);
					}
					
				}
			}
		}
		
		
		//入库的内容
		$data_content = "";
		
		preg_match_all("/\<div class=\"dTitle\"\>.*?href=\"(.*?)\".*?title/is",$content,$match);
		
		
		
		
		if($match[1])
		{	
			$match[1] = array_reverse($match[1]);
			foreach($match[1] as $key => $value)
			{
				echo $value."<br>\n";
				$insert_res = $this->insert_gonglue_cron_list($value,$topic_id,1);//级别1
				
				if(!$insert_res)
				{
					continue;
				}
				
				
				//抓第一条内容
				$detail = file_get_contents($value);
				if(!$detail)
				{
					continue;
				}
				
				
				preg_match_all("/\<div.*?=\"artArea\".*?\>(.*?)\<\/div\>[\s]+\<\/?div/is",$detail,$match1);
				
				//内容第一页内容
				$data_content = "";
				$length_get=0;
				foreach($match1[1] as $value_get)
				{
					$value_get = preg_replace("/\<a.*?onclick.*?a\>/is","",$value_get);
					$value_get = preg_replace("/\<script.*?script\>/is","",$value_get);
					$word_get_length = strlen(preg_replace("/\<.*?\>/","",$value_get));
					
					
					if($word_get_length > $length_get)
					{
						$data_content = $value_get;
						$length_get = $word_get_length;
					}
				}
				
				
				
				//匹配分页
				$chck_url = preg_quote(substr($value,0,strlen($value)-5),"/");
				preg_match_all("/({$chck_url}_.*?html)/is",$detail,$match2);
				$get_all_page = array_unique($match2[1]);
				
				
				
				
				//遍历所有分页，取出内容
				foreach($get_all_page as $key1 => $value1)
				{
					
					$page_detail = file_get_contents($value1);
					preg_match_all("/\<div.*?=\"artArea\".*?\>(.*?)\<\/div\>[\s]+\<\/?div/is",$page_detail,$match1);
					$long_content ="";
					$length_get=0;
					foreach($match1[1] as $value3)
					{
						$value3 = preg_replace("/\<a.*?onclick.*?a\>/is","",$value3);
						$value3 = preg_replace("/\<script.*?script\>/is","",$value3);
						$length=strlen(preg_replace("/\<.*?\>/","",$value3));
						$word_get_length = $length;
						if($word_get_length < 300)
						if($word_get_length > $length_get)
						{
							$long_content = $value3;
							$length_get = $word_get_length;
						}
					}
					$data_content .= $long_content;
				}
				
				
					
				//删除链接
				//$data_content = preg_replace("/\<a.*?\>/","",$data_content);
				//$data_content = preg_replace("/\<\/a.*?\>/","",$data_content);
				$data_content = $this->replace_a($data_content);
				
				
				preg_match("/\<div class=\"artCon\"\>.*?\<h1\>(.*?)\<\/h1\>/is",$detail,$match_title);
				$title = $match_title[1];
				
				preg_match("/([0-9]{4}-[0-9]{2}\-[0-9]{2} [0-9]{2}:[0-9]{2})/is",$detail,$match_time);
				$time= $match_time[1];
				
				
				
				$data_content = $this->replace_pic($data_content);
				
				$data_content = str_replace("pcgames.com.cn","youximeitu.com",$data_content);
					
				$this->insert_gonglue($value,$topic_id,$title,$data_content,$content_type,$time);

			}
		}
		else
		{
		
			echo "not match <br>\n";
		}
		
		
		
	}
	
	function replace_pic($data_content)
	{
		preg_match_all("/\<img.*?src=\"(.*?)\"/is",$data_content,$match_pic);
		preg_match_all("/=\"\s*(http:\/\/img[0-9]+\.pcgames\.com\.cn\/.*?)\"/is",$data_content,$match_pic_other);
		
		if($match_pic[1] && $match_pic_other[1])
		{
			$pic_need_replace = array_merge($match_pic[1],$match_pic_other[1]);
		}
		else
		{
			if($match_pic[1] )
			{
				$pic_need_replace = $match_pic[1];
			}
			
			if($match_pic_other[1] )
			{
				$pic_need_replace = $match_pic_other[1];
			}
			
		}
		$pic_need_replace = array_unique($pic_need_replace);
		//获取需要替换的图片
		
		$get_pcgame_pic = $this->get_pcgame_pic($pic_need_replace);
		
		
		
		$data_content = str_replace($match_pic[1],$get_pcgame_pic,$data_content);
		
		return $data_content;
		
	}
	
	function replace_a($content)
	{
		$content = preg_replace("/\<\/?div.*?\>/","",$content);
		
		preg_match_all("/\<a.*?href=\"(.*?)\".*?\>/is",$content,$match);
		$db_base = new db_base();
		
		
		if($get_url = $match[1])
		{
			$get_unique_url= array_unique($get_url);
			
			$md5_url = array();
			foreach($get_unique_url as $key => $value)
			{
				$md5_url[] = "'".md5($value)."'";
			}
			
			$sql = "select id,url ,md5_url from gonglue where md5_url in ( ".join(",",$md5_url).") ";
			
			$get_list = $db_base->select($sql);
			
			$get_md5_url = array();
			$md5_url_hash = array();
			foreach($get_list as $value)
			{
				$get_md5_url[] = $value["md5_url"];
				$md5_url_hash[$value["md5_url"]] =  $value["id"];
			}
			
			
			$replace_array = array();
			//var_dump($get_md5_url);
			foreach($get_url as $key => $value)
			{
				$md5_key = md5($value);
				//var_dump($md5_key);
				if(in_array($md5_key,$get_md5_url))//如果在本地已经有了，使用本地链接
				{
					$replace_array[$key] = "/gonglue/detail{$md5_url_hash[$md5_key]}.html";
				}
				else
				{
					$replace_array[$key] = "";
				}
				
			}
			
		}
	
		
		if($replace_array)
		{
			$content = str_replace($get_url,$replace_array,$content);
				
			return	 $content;
		}
		else
		{
			
			return $content;
		}
		
		
		
	}
	
	function get_pcgame_pic($url_array)
	{
		$return_array = array();
		foreach($url_array as $value)
		{
			$get_pic_ulr = $this->get_pic_from_url($value);
			if(!$get_pic_ulr)
			{
				$return_array[] = $value;
			}
			else
			{
				$return_array[] = $get_pic_ulr;
			}
		}
			
		return $return_array;
	}
	
	
	function get_pic_from_url($url)
	{
		
		$pic_upload = new pic_upload();
		$url_new = $pic_upload->get_sina_pic_by_url($url);
	
		
		return $url_new;
		
	}
	
	function insert_gonglue_cron_list($url,$topic_id,$level)
	{
		$domain_info = parse_url($url);
		$domain = $domain_info["host"];
		$md5_url = md5($url);
		$ary["url"] = $url;
		$ary["domain"] = $domain;
		$ary["topic_id"] = $topic_id;
		$ary["level"] = $level;
		$ary["md5_url"] = $md5_url;
		$ary["create_time"] = date::get_date_time();
		$ary["status"] = 1;
		
		$db_base = new db_base();
		$res = $db_base->insert("gonglue_cron_list",$ary);
		
		return $res;
		
	}
	
	
	function insert_gonglue($url,$topic_id,$title,$content,$content_type,$time)
	{
		if(!$content)
		{
			return false;
		}
		if(strlen($content) < 20)
		{
			return false;
		}
		
		if(preg_match("/src=['\"\s]?http[\S]+swf/i",$content))
		{
			$content_type=1;
		}
		else
		{
			$content_type=0;
		}
		$content = trim($content);
		$title = mb_convert_encoding($title, "UTF-8", "GBK");
		$content = mb_convert_encoding($content, "UTF-8", "GBK");
		
		$domain_info = parse_url($url);
		$domain = $domain_info["host"];
		$md5_url = md5($url);
		$ary["url"] = $url;
		$ary["domain"] = $domain;
		$ary["topic_id"] = $topic_id;
		$ary["md5_url"] = $md5_url;
		
		$ary["title"] = $title;
		$ary["content"] = $content;
		$ary["content_type"] = $content_type;
				
		
		$ary["create_date"] = date("Y-m-d",strtotime($time));
		$ary["create_time"] = $time;
		$ary["status"] = 1;
		
		$db_base = new db_base();
		$res = $db_base->insert("gonglue",$ary);
		
		return $res;
		
	}
}
mod_cron_engine_main::get_test($_GET["url"]);


